﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
using System.Drawing.Printing;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace RDLCPrinter
{
    public class clsReportPrint
    {

        private int m_currentPageIndex;    
        private IList<Stream> m_streams;  
        private DataTable LoadSalesData(string sqlstr)    
        {    
            SqlConnection con = new SqlConnection();  
            SqlCommand cmd = new SqlCommand(sqlstr, con);  
            SqlDataAdapter adp = new SqlDataAdapter();  
            adp.SelectCommand = cmd;  
            con.Open();  
            DataTable dt = new DataTable();  
            adp.Fill(dt);  
            con.Close();  
            return dt; 
        }  
  
  
  
        private Stream CreateStream(string name, string fileNameExtension,   
         Encoding encoding, string mimeType, bool willSeek)    
        {    
            Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);   
            m_streams.Add(stream);    
            return stream;    
        }  
  
  
  
        private void Export(LocalReport report)    
        {    
            string deviceInfo =    
              "<DeviceInfo>" +    
              "  <OutputFormat>EMF</OutputFormat>" + 
                //"  <PageWidth>8.5in</PageWidth>" +  
                //"  <PageHeight>11in</PageHeight>" +  
                //"  <MarginTop>0.25in</MarginTop>" +  
                //"  <MarginLeft>0.25in</MarginLeft>" +  
                //"  <MarginRight>0.25in</MarginRight>" +  
                //"  <MarginBottom>0.25in</MarginBottom>" +  
                "</DeviceInfo>";    
            Warning[] warnings;    
            m_streams = new List<Stream>();    
            try    
           {    
                report.Render("Image", deviceInfo, CreateStream, out warnings);  
           }  
           catch (Exception ex)  
           {  
                  Exception innerEx = ex.InnerException;//取内异常。因为内异常的信息才有用，才能排除问题。  
                  while (innerEx != null)  
                 {  
                      MessageBox.Show(innerEx.Message);  
                      innerEx = innerEx.InnerException;  
                 }  
            }  
           foreach (Stream stream in m_streams)  
                stream.Position = 0;  
       }  
  
  
  
        private void PrintPage(object sender, PrintPageEventArgs ev)  
        {
              Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); 
              Rectangle adjustedRect = new Rectangle(
                  ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
                  ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
                  ev.PageBounds.Width,
                  ev.PageBounds.Height);
              ev.Graphics.DrawImage(pageImage, adjustedRect);
              m_streams[m_currentPageIndex].Close(); 
              m_currentPageIndex++;  
              ev.HasMorePages = (m_currentPageIndex < m_streams.Count);  
         }  
  
  
  
        public void Print()  
        {
            string printerName ;  
            if (m_streams == null || m_streams.Count == 0)  
                return;
            PrintDocument printDoc = new PrintDocument(); 
            printerName = printDoc.PrinterSettings.PrinterName; 
            if (!printDoc.PrinterSettings.IsValid)  
            {  
                 string msg = String.Format("Can't find printer : "+ printerName);  
                 Debug.WriteLine(msg);  
                 return;  
             }  
             printDoc.PrintPage += new PrintPageEventHandler(PrintPage);  
             printDoc.Print();  
         }


        public LocalReport report = new LocalReport();
        public  void Run(string sqlstr,string  reportname)  
        {             
             report.ReportPath = Application.StartupPath + "\\"+reportname;//加上报表的路径  
             report.DataSources.Add(new ReportDataSource("DataSet1", LoadSalesData(sqlstr)));
             //ReportParameter HospitalName = new ReportParameter("HospitalName", Param.HospitalName + "处方笺");
             //report.SetParameters(new ReportParameter[] { HospitalName });
             //ReportParameter TotalMoney = new ReportParameter("TotalMoney", money.ToString());
             //report.SetParameters(new ReportParameter[] { TotalMoney });
             Export(report);  
             m_currentPageIndex = 0;  
             //Print();  
         }

        public void Dispose()
        {
            if (m_streams != null)
            {
                foreach (Stream stream in m_streams)
                    stream.Close();
                m_streams = null;
            }
        }

        
    }
}
